package com.yunfeisoft.business.controller;

import com.yunfeisoft.base.enumeration.YesNoEnum;
import com.yunfeisoft.base.model.Attachment;
import com.yunfeisoft.base.model.User;
import com.yunfeisoft.base.service.inter.AttachmentService;
import com.yunfeisoft.business.model.Elder;
import com.yunfeisoft.business.model.ElderApprovalResult;
import com.yunfeisoft.business.model.OutHospital;
import com.yunfeisoft.business.model.OutHospitalApprovalResult;
import com.yunfeisoft.business.service.inter.ElderApprovalResultService;
import com.yunfeisoft.business.service.inter.ElderService;
import com.yunfeisoft.business.service.inter.OutHospitalApprovalResultService;
import com.yunfeisoft.business.service.inter.OutHospitalService;
import com.yunfeisoft.common.base.BaseController;
import com.yunfeisoft.common.utils.Page;
import com.yunfeisoft.common.utils.Response;
import com.yunfeisoft.common.utils.ResponseUtils;
import com.yunfeisoft.common.utils.Validator;
import com.yunfeisoft.utils.ApiUtils;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.util.ObjectUtils;
import org.springframework.web.bind.ServletRequestUtils;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.bind.annotation.RestController;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * ClassName: OutHospitalController
 * Description: 老人出院流转表Controller
 * Author: Jackie liu
 * Date: 2020-01-13
 */
@RestController
public class OutHospitalController extends BaseController {

    @Autowired
    private OutHospitalService outHospitalService;

    @Autowired
    private ElderService elderService;

    @Autowired
    private ElderApprovalResultService elderApprovalResultService;

    @Autowired
    private OutHospitalApprovalResultService outHospitalApprovalResultService;

    @Autowired
    private AttachmentService attachmentService;

    /**
     * 添加老人出院流转表
     *
     * @param record
     * @param request
     * @param response
     * @return
     */
    @RequestMapping(value = "/web/outHospital/save", method = RequestMethod.POST)
    public Response save(OutHospital record, HttpServletRequest request, HttpServletResponse response) {
        // 验证参数
        Validator validator = new Validator();
        validator.required(request, "elderId", "老人为空");
        validator.required(request, "leaveDate", "离院日期为空");
        validator.required(request, "reason", "离院原因为空");
        validator.required(request, "remark", "备注为空");
        if (validator.isError()) {
            ResponseUtils.warn(validator.getMessage());
        }
        // 默认为登记状态
        record.setStatus(OutHospital.OutHospitalStatusEnum.REGISTER.getValue());
        outHospitalService.save(record);
        return ResponseUtils.success("保存成功");
    }

    /**
     * 修改老人出院流转表
     *
     * @param record
     * @param request
     * @param response
     * @return
     */
    @RequestMapping(value = "/web/outHospital/modify", method = RequestMethod.POST)
    public Response modify(OutHospital record, HttpServletRequest request, HttpServletResponse response) {
        Validator validator = new Validator();
        validator.required(request, "id", "参数错误");
        validator.required(request, "elderId", "老人为空");
        validator.required(request, "leaveDate", "离院日期为空");
        validator.required(request, "reason", "离院原因为空");
        validator.required(request, "remark", "备注为空");
        if (validator.isError()) {
            return ResponseUtils.warn(validator.getMessage());
        }
        outHospitalService.modify(record);
        return ResponseUtils.success("保存成功");
    }

    /**
     * 查询老人出院流转表
     *
     * @param request
     * @param response
     * @return
     */
    @RequestMapping(value = "/web/outHospital/query", method = {RequestMethod.POST, RequestMethod.GET})
    public Response query(HttpServletRequest request, HttpServletResponse response) {
        String id = ServletRequestUtils.getStringParameter(request, "id", null);
        if (StringUtils.isBlank(id)) {
            return ResponseUtils.warn("参数错误");
        }
        Map<String, Object> result = new HashMap<>();
        OutHospital record = outHospitalService.load(id);
        // 查询老人信息
        if (record != null) {
            Elder elder = elderService.load(record.getElderId());
            record.setElderName(elder.getName());
            // 查询老人审批记录列表
            List<ElderApprovalResult> approvalResultList = elderApprovalResultService.queryByElderId(elder.getId());
            if (!ObjectUtils.isEmpty(approvalResultList)) {
                for (ElderApprovalResult elderApprovalResult : approvalResultList) {
                    // 查询该审批记录的附件
                    List<Attachment> attachments = attachmentService.queryByRefId(elderApprovalResult.getId());
                    elderApprovalResult.setAttachmentList(attachments);
                }
            }
            result.put("approvalResultList", approvalResultList);
            // 查询出院审批记录
            List<OutHospitalApprovalResult> outHospitalApprovalResultList = outHospitalApprovalResultService.queryListByOutId(record.getId());
            if (!ObjectUtils.isEmpty(outHospitalApprovalResultList)) {
                for (OutHospitalApprovalResult outHospitalApprovalResult : outHospitalApprovalResultList) {
                    // 查询该审批记录的附件
                    List<Attachment> attachments = attachmentService.queryByRefId(outHospitalApprovalResult.getId());
                    outHospitalApprovalResult.setAttachmentList(attachments);
                }
            }
            result.put("outHospitalApprovalResultList", outHospitalApprovalResultList);
            result.put("elder", elder);
        }
        result.put("record", record);
        return ResponseUtils.success(result);
    }

    /**
     * 分页查询老人出院流转表
     *
     * @param request
     * @param response
     * @return
     */
    @RequestMapping(value = "/web/outHospital/list", method = {RequestMethod.POST, RequestMethod.GET})
    @ResponseBody
    public Response list(HttpServletRequest request, HttpServletResponse response) {
        // 获取姓名
        String elderName = ServletRequestUtils.getStringParameter(request, "elderName", null);
        int status = ServletRequestUtils.getIntParameter(request, "status", -1);
        Map<String, Object> params = new HashMap<String, Object>();
        initParams(params, request);
        params.put("elderName", elderName);
        if (status > 0) {
            params.put("status", status);
        }

        User user = ApiUtils.getLoginUser();
        if (user.getIsSys() != YesNoEnum.YES_ACCPET.getValue()) {
            params.put("orgId", user.getOrgId());
        }

        Page<OutHospital> page = outHospitalService.queryPage(params);
        return ResponseUtils.success(page);
    }

    /**
     * 批量删除老人出院流转表
     *
     * @param request
     * @param response
     * @return
     */
    @RequestMapping(value = "/web/outHospital/delete", method = {RequestMethod.POST, RequestMethod.GET})
    @ResponseBody
    public Response delete(HttpServletRequest request, HttpServletResponse response) {
        String ids = ServletRequestUtils.getStringParameter(request, "ids", null);
        if (StringUtils.isBlank(ids)) {
            return ResponseUtils.warn("参数错误");
        }
        outHospitalService.remove(ids.split(","));
        return ResponseUtils.success("删除成功");
    }

    /**
     * 出院院办存档
     *
     * @param ids
     * @param request
     * @param response
     * @return
     */
    @RequestMapping(value = "/web/outHospital/hospitalArchive", method = RequestMethod.POST)
    public Response hospitalArchive(String ids, HttpServletRequest request, HttpServletResponse response) {
        if (StringUtils.isBlank(ids)) {
            return ResponseUtils.warn("参数错误");
        }
        String[] idArr = ids.split(",");
        outHospitalService.hospitalArchive(idArr);
        return ResponseUtils.success("保存成功");
    }
}
